package student.view;

import handler.LoginHandler;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class LoginView extends JFrame {

    JLabel nameLable =new JLabel("学生成绩管理系统",JLabel.CENTER);
    SpringLayout springLayout=new SpringLayout();
    JPanel centerPanel=new JPanel(springLayout);
    JLabel userNameLablel=new JLabel("用户名");
    JTextField userTxt=new JTextField();
    JLabel pwLabel=new JLabel("密码");
    JPasswordField pwTxt=new JPasswordField();
    JButton loginButton=new JButton("登录");
    JButton reseButton=new JButton("重置");
    LoginHandler loginHandler;
    public LoginView(){
        super("学生成绩管理系统");
        loginHandler=new LoginHandler(this);
        Container contentPane=getContentPane();
        nameLable.setFont(new Font("华文行楷",Font.PLAIN,40));
        nameLable.setPreferredSize(new Dimension(0,80));
        Font centerFont=new Font("楷体",Font.PLAIN,20);
        userNameLablel.setFont(centerFont);
        pwLabel.setFont(centerFont);
        loginButton.setFont(centerFont);
        reseButton.setFont(centerFont);
        userTxt.setPreferredSize(new Dimension(200,30));
        pwTxt.setPreferredSize(new Dimension(200,30));
        centerPanel.add(userNameLablel);
        centerPanel.add(userTxt);
        centerPanel.add(pwLabel);
        centerPanel.add(pwTxt);
        loginButton.addActionListener(loginHandler);
        loginButton.addKeyListener(loginHandler);
        centerPanel.add(loginButton);
        reseButton.addActionListener(loginHandler);
        centerPanel.add(reseButton);
        Spring childWidth=Spring.sum(Spring.sum(Spring.width(userNameLablel),Spring.width(userTxt)),Spring.constant(20));
        int offsetX=childWidth.getValue()/2;
        springLayout.putConstraint(SpringLayout.WEST,userNameLablel,-offsetX,springLayout.HORIZONTAL_CENTER,centerPanel);
        springLayout.putConstraint(SpringLayout.NORTH,userNameLablel,20,SpringLayout.NORTH,centerPanel);
        springLayout.putConstraint(SpringLayout.WEST,userTxt,20,SpringLayout.EAST,userNameLablel);
        springLayout.putConstraint(SpringLayout.NORTH,userTxt,0,SpringLayout.NORTH,userNameLablel);
        springLayout.putConstraint(SpringLayout.EAST,pwLabel,0,SpringLayout.EAST,userNameLablel);
        springLayout.putConstraint(SpringLayout.NORTH,pwLabel,20,SpringLayout.SOUTH,userNameLablel);
        springLayout.putConstraint(SpringLayout.NORTH,pwTxt,0,SpringLayout.NORTH,pwLabel);
        springLayout.putConstraint(SpringLayout.WEST,pwTxt,20,SpringLayout.EAST,pwLabel);
        springLayout.putConstraint(SpringLayout.WEST,loginButton,50,SpringLayout.WEST,pwLabel);
        springLayout.putConstraint(SpringLayout.NORTH,loginButton,20,SpringLayout.SOUTH,pwLabel);
        springLayout.putConstraint(SpringLayout.WEST,reseButton,80,SpringLayout.WEST,loginButton);
        springLayout.putConstraint(SpringLayout.NORTH,reseButton,20,SpringLayout.SOUTH,pwLabel);
        contentPane.add(nameLable,BorderLayout.NORTH);
        contentPane.add(centerPanel,BorderLayout.CENTER);
        getRootPane().setDefaultButton(loginButton);
        setSize(600,400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);
    }
    public static void main(String[] args) {
        new LoginView();
    }

    public JTextField getUserTxt() {
        return userTxt;
    }
    public JPasswordField getPwTxt() {
        return pwTxt;
    }
}
